#include "TanPainter.h"
#include "core/Tan.h"
#include <QPaintDevice>
#include <core/CoordinateTransformer.h>

namespace qtangram {
    TanPainter::TanPainter(QPaintDevice* widget) : QPainter(widget) {}
    void TanPainter::drawTan(Tan* tan) {
        setPen(tan->getBackgroundColor());
        if(tan->selected) setBrush(tan->getBackgroundColor().lighter(200));
        else setBrush(tan->getBackgroundColor());
        drawPolygon(tan->polyScreen);
    }

    void TanPainter::drawTan(Tan* tan, QPoint translation) {
        setPen(tan->getBackgroundColor());
	setBrush(tan->getBackgroundColor());
        drawPolygon(tan->polyScreen.translated(translation));
    }

    void TanPainter::drawTanContur(Tan* tan, QPoint translation) {
        setPen(Qt::black);
        setBrush(Qt::transparent);
        drawPolygon(tan->polyScreen.translated(translation));
    }

    void TanPainter::drawBounds(ContainerWidget* cw)
    {
        setPen(Qt::black);
        setBrush(Qt::transparent);

        QPointF vtl(-cw->getViewRange()/2.0,cw->getViewRange()/2.0);
        QPointF vbr(cw->getViewRange()/2.0,-cw->getViewRange()/2.0);
        QPoint tl(CoordinateTransformer::worldToScreen(cw,vtl));
        QPoint br(CoordinateTransformer::worldToScreen(cw,vbr));
        tl.setX(tl.x()+1); tl.setY(tl.y()+1);
        br.setX(br.x()-2); br.setY(br.y()-2);
        drawRect(QRect(tl,br));
    }

    void TanPainter::drawSelectionArea(QRect sa)
    {
        QPen pen(Qt::red);
        pen.setStyle(Qt::DashLine);
        setPen(pen);
        setBrush(Qt::transparent);
        drawRect(sa);
    }

}
